package com.example.keepmesafe.dialogs;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.keepmesafe.R;
import com.example.keepmesafe.activities.UserHome;
import com.example.keepmesafe.domain.Tag;
import com.example.keepmesafe.repository.DBHelper;
import com.example.keepmesafe.service.AESCrypto;
import com.example.keepmesafe.service.Security;

@SuppressLint("ValidFragment")
public class ShareTagDialog extends DialogFragment{
	private DBHelper tagStorageImpl;
	private Context ctx;
	private Tag tagToShare;
	private Activity parentActivity;
	
	public ShareTagDialog(){}
	
	public ShareTagDialog(Activity parentActivity,Context ctx,Tag t) {
		this.tagToShare = t;
		this.ctx = ctx;
		this.parentActivity = parentActivity;
	}


	@Override
	    public Dialog onCreateDialog(Bundle savedInstanceState) {
			final SmsManager smsManager = SmsManager.getDefault();
			  final AESCrypto crypto = new AESCrypto();
			
	        // Use the Builder class for convenient dialog construction
	        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
	        LayoutInflater inflater = LayoutInflater.from(ctx);
		    final View dialogView = inflater.inflate(R.layout.share_tag, null);
		    builder.setView(dialogView)
	               .setPositiveButton(R.string.btnShareTag, new DialogInterface.OnClickListener() {
	                   public void onClick(DialogInterface dialog, int id) {
	                	   try {
	                		   EditText editText = (EditText)((AlertDialog)dialog).findViewById(R.id.shareTagCellNo);
	                		   String cell = editText.getText().toString();
	                		   String formattedCellNumber = "+27"+cell.substring(1, cell.length());
	                		   Toast.makeText(ctx, "Sending To: " +formattedCellNumber, Toast.LENGTH_SHORT).show();
	                		   smsManager.sendTextMessage(formattedCellNumber, null,"The following tag was sent to you:\nTag Name: "+ tagToShare.getTagName()+"\nTag Username: " + tagToShare.getTagUsername()+ "\nTag Password: "+ crypto.decrypt(tagToShare.getTagPassword()), null, null);
	                	   }catch(Exception e){
	                		   Toast.makeText(ctx,"SMS faild, please try again.",Toast.LENGTH_LONG).show();
	                		   e.printStackTrace();
	                	   }
	                	   UserHome uh  = (UserHome)parentActivity;
	                	   uh.refreshList();
	                   }
	               })
	               .setNegativeButton(R.string.btnCancel, new DialogInterface.OnClickListener() {
	                   public void onClick(DialogInterface dialog, int id) {
	                	   dialog.dismiss();
	                	   UserHome uh  = (UserHome)parentActivity;
	                	   uh.refreshList();
	                   }
	               });
	        // Create the AlertDialog object and return it
	        return builder.create();
	    }
}
